# Distributed under MIT License - See LICENSE
require 'family_tree_api/client'

# For session handling, if you have an active session, 
# go ahead and insert it in here. Otherwise, set session = nil and
# it will use the ft.authenticate to authenticate you.
#session='USYS2AADBB47B3A6043209CBE16725938464.ptap009-035'

client = FamilyTreeApi::Client.new 'https://api.familysearch.org', 'WCQY-7J1Q-GKVV-7DNM-SQ5M-9Q5H-JX3H-CMJK'

begin

  client.authenticate('[username]','[password]') # => throws an exception if unable to authenticate. returns true if successful.
  puts client.session # => 'USYS2AADBB47B3A6043209CBE16725938464.ptap009-035' or something similar
  
  # Search Service Usage Example
  puts "=== Search ==="
  results = client.search :givenName => "Francis", :familyName => "Zimmerman", :maxResults => 5
  results.each do |r|
    puts "Score: #{r.score}"
    puts "#{r.person.name} (#{r.person.gender}) - #{r.person.ref}"
    r.person.events.each do |event|
      puts "#{event.type}: #{event.date} #{'in '+event.place if event.place}"
    end
  end
  
# Person Read Service Usage
  puts "=== Person Read ==="
  person = client.person 'KWZF-CFW', {:ancestors => 1, :descendants => 1}
  puts person.full_names.first
  puts "F:#{person.father.full_names.first}"
  puts "M:#{person.mother.full_names.first}"
  person.children.each do |child|
   puts "C:#{child.full_names.first}"
  end
  
rescue Exception => e
  puts e.message
  puts e.backtrace
end